<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.jack.bookshop.vo.UserVo" %>
<%@ page import="com.jack.bookshop.vo.CartVo" %>
<%@ page import="com.jack.bookshop.dao.CartDao" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 炜杰
  Date: 2023/6/21
  Time: 16:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<%
    UserVo userVo = (UserVo) session.getAttribute("userVo");
    List<CartVo> list = new CartDao().getList(userVo.getId());
    pageContext.setAttribute("list", list);
%>
<html>
<head>
    <title>Jack's Bookshop</title>
    <%@ include file="resource.jsp" %>
</head>
<body>
<%@ include file="header.jsp" %>
<table class="table form-inline">
    <tr>
        <td>No</td>
        <td></td>
        <td>Title</td>
        <td>Price</td>
        <td width="160px">Quantity</td>
        <td width="80px">Total</td>
        <td></td>
    </tr>
    <c:forEach var="cart" items="${pageScope.list}" varStatus="status">
        <tr>
            <td>${status.count}</td>
            <td>
                <input id="${cart.id}" onclick="handleclick(event)" class="form-check-input" type="checkbox" data-id="${cart.id}" ${cart.checked==1?"checked":""} />
            </td>
            <td>${cart.book_title}</td>
            <td class="price_td">${cart.book_price}</td>
            <td>
                <a href="cart.do?action=update&id=${cart.id}&quantity=-1" class="btn btn-primary <c:if test="${cart.quantity==1}">disabled</c:if>">-</a>
                <input value="${cart.quantity}" class="form-control text-center"
                       style="width:48px;display:inline-block;"/>
                <a href="cart.do?action=update&id=${cart.id}&quantity=1" class="btn btn-primary">+</a>
            </td>
            <td class="amount_td text-right">
                    ${cart.book_price*cart.quantity}
            </td>
            <td>
                <a href="cart.do?action=delete&id=${cart.id}" class="btn btn-danger">remove</a>
            </td>
        </tr>
    </c:forEach>
<%--    <tr>--%>
<%--        <td colspan="6" style="text-align:right;">Total</td>--%>
<%--        <td class="text-bold text-right">--%>
<%--            <div id="total_p">0</div>--%>
<%--        </td>--%>
<%--    </tr>--%>
</table>
<p>&nbsp;</p>
<p class="text-center">
    <a href="PayServlet" class="btn btn-lg btn-primary">Pay Right Now</a>
</p>
<!-- 操作反馈 bootstrap toast -->
<div class="toast-container position-fixed bottom-0 end-0 p-3">
    <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
            <strong class="me-auto">${param.title}</strong>
            <small>1 mins ago</small>
            <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        <div class="toast-body">
            ${param.msg}
        </div>
    </div>
</div>
<!-- 导入页脚 -->
<%@ include file="footer.jsp" %>
</body>
</html>
<script>
    const toastLiveExample = document.getElementById('liveToast');
    const toast = new bootstrap.Toast(toastLiveExample);
    if (${param.msg!=null}) {
        toast.show()
    }

    function handleclick(event) {
        const clickedElement = event.target;
        if(clickedElement.checked){
            window.location.href = "cart.do?action=checked&id="+clickedElement.id+"&checked=1";
        }else{
            window.location.href = "cart.do?action=checked&id="+clickedElement.id+"&checked=0";
        }
    }
</script>

